﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraSplashScreen;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Helper;
using XCI.Sys;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;

namespace XCI.Windows
{
    /// <summary>
    /// 角色下拉多选控件
    /// </summary>
    [System.ComponentModel.DesignerCategory("Code")]
    public class RoleCheckedComboBoxEdit : CheckedComboBoxEdit, IAsyncBindingData
    {
        static RoleCheckedComboBoxEdit()
        {
            RepositoryItemRoleCheckedComboBoxEdit.RegisterRoleCheckedComboBoxEdit();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemRoleCheckedComboBoxEdit Properties => base.Properties as RepositoryItemRoleCheckedComboBoxEdit;

        public override string EditorTypeName => RepositoryItemRoleCheckedComboBoxEdit.CustomEditName;

        public RoleCheckedComboBoxEdit()
        {

        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
            base.Properties.ValueMember = nameof(SysRole.Id);
            base.Properties.DisplayMember = nameof(SysRole.Name);
        }

        /// <summary>
        /// 数据异步绑定
        /// </summary>
        /// <returns>等待的任务</returns>
        public async Task BindingDataAsync()
        {
            var deptId = SysApp.CurrentUser.IsAdmin ? null : SysApp.CurrentUser.DeptId;
            var roleList = await SysRoleService.Instance.SelectListAsync(new SysRoleFilter
            {
                Status = true,
                EnableDeptScope = true,
                DeptId = deptId
            });
            base.Properties.DataSource = roleList;
        }
    }

    [UserRepositoryItem("RegisterRoleCheckedComboBoxEdit")]
    public class RepositoryItemRoleCheckedComboBoxEdit : RepositoryItemCheckedComboBoxEditx
    {
        static RepositoryItemRoleCheckedComboBoxEdit()
        {
            RegisterRoleCheckedComboBoxEdit();
        }

        public new const string CustomEditName = "RoleCheckedComboBoxEdit";

        public RepositoryItemRoleCheckedComboBoxEdit()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterRoleCheckedComboBoxEdit()
        {
            Image img = null;
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(RoleCheckedComboBoxEdit), typeof(RepositoryItemRoleCheckedComboBoxEdit), typeof(RoleCheckedComboBoxEditViewInfo), new RoleCheckedComboBoxEditPainter(), true, img));
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                if (!(item is RepositoryItemRoleCheckedComboBoxEdit source)) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }
    }

    public class RoleCheckedComboBoxEditViewInfo : CheckedComboBoxEditxViewInfo
    {
        public RoleCheckedComboBoxEditViewInfo(RepositoryItem item) : base(item)
        {
        }
    }

    public class RoleCheckedComboBoxEditPainter : CheckedComboBoxEditxPainter
    {
        public RoleCheckedComboBoxEditPainter()
        {
        }
    }
}